<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 01:16:57
 * @LastEditTime: 2023-09-18 17:20:13
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use App\Traits\AutoCodeTrait;

class Material extends BaseModel
    {

    use AutoCodeTrait;
    protected $ACSign = 'MATERIAL_CODE';
    public $timestamps = true;
    public $table = "basedata_material";
    protected $guarded = [];
    protected $appends = [];
    public function category()
        {
        return $this->belongsTo(MaterialCategory::class);
        }


    public function getNameAttribute($value)
        {
        $newname = $this->attributes['name'];
        if (app("sys")->is_inspect_factory === 1) {
            if (isset($this->attributes['inspectionname'])) {
                $newname = $this->attributes['inspectionname'];
                }
            else {
                $this->refresh();
                $newname = $this->attributes['inspectionname'];
                }
            if (isEmpty($newname)) {
                $newname = $this->attributes['name'];
                }
            }
        return $newname;
        }
    public function getFullnameAttribute($value)
        {
        $newname = $this->attributes['fullname'];
        if (app("sys")->is_inspect_factory === 1) {
            if (isset($this->attributes['inspectionname'])) {
                $newname = $this->attributes['inspectionname'];
                }
            else {
                $this->refresh();
                $newname = $this->attributes['inspectionname'];
                }
            if (isEmpty($newname)) {
                $newname = $this->attributes['fullname'];
                }
            }
        return $newname;
        }
    public function unit()
        {
        return $this->belongsTo(Dic::class, "unit_id", "key")->where(function ($q)
            {
            $q->where("code", 'unit');
            });
        }
    public function unit1()
        {
        return $this->belongsTo(Dic::class, "unit1_id", "key")->where(function ($q)
            {
            $q->where("code", 'unit');
            });
        }
    public function film_category()
        {
        return $this->belongsTo(Dic::class, "film_category_id", "key")->where(function ($q)
            {
            $q->where("code", 'FilmCategory');
            });
        }
    public function uunit()
        {
        return $this->belongsTo(Dic::class, "uunit_id", "key")->where(function ($q)
            {
            $q->where("code", 'unit');
            });
        }
    public function ounit()
        {
        return $this->belongsTo(Dic::class, "ounit_id", "key")->where(function ($q)
            {
            $q->where("code", 'unit');
            });
        }
    public function material_nature()
        {
        return $this->belongsTo(Dic::class, "material_nature_id", "key")->where(function ($q)
            {
            $q->where("code", 'MaterialNature');
            });
        }
    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'status_name');
            });
        }

    public function lot()
        {
        return $this->hasMany(MpcLot::class, "material_id")->with(['company']);
        }
    }